package com.guqj.mq.rabbitmq;

import java.io.IOException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;

public class RabbitClient {

	public static void main(String[] args) {
		receive();
	}

	public static void receive() {
		Connection conn = null;
		Channel channel = null;
		try {
			// 建立连接
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("localhost");
			//factory.setVirtualHost("test_host");
			conn = factory.newConnection();
			channel = RabbitUtil.createChannel(conn);
			if (conn != null && channel != null) {
				QueueingConsumer consumer = new QueueingConsumer(channel);
				channel.basicConsume(RabbitConfig.QUEUENAME, true, consumer);
				while (true) {
					// 获取消息，如果没有消息，这一步将会一直阻塞
					Delivery delivery = consumer.nextDelivery();
					String msg = new String(delivery.getBody());
					System.out.println("received message[" + msg + "] from " + RabbitConfig.QUEUENAME + " TIME " +System.currentTimeMillis());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RabbitUtil.close(conn, channel);
		}
	}
	
	public static void receiveAck() {
		try {
			// 建立连接
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("localhost");
			//factory.setVirtualHost("test_host");
			Connection conn = factory.newConnection();
			final Channel channel = RabbitUtil.createChannel(conn);
			if (conn != null && channel != null) {
				QueueingConsumer consumer = new QueueingConsumer(channel);
				boolean autoAck = true;
				channel.basicConsume(RabbitConfig.QUEUENAME, autoAck, "myConsumerTag",
				     new DefaultConsumer(channel) {
				         @Override
				         public void handleDelivery(String consumerTag,
				                                    Envelope envelope,
				                                    AMQP.BasicProperties properties,
				                                    byte[] body)
				             throws IOException
				         {
				             String routingKey = envelope.getRoutingKey();
				             String contentType = properties.getContentType();
				             long deliveryTag = envelope.getDeliveryTag();
				             System.out.println("routingKey:["+routingKey+"] contentType:["+contentType+"] deliveryTag:["+deliveryTag+"]");
				             channel.basicAck(deliveryTag, false);
				         }
				     });
				while (true) {
					// 获取消息，如果没有消息，这一步将会一直阻塞
					Delivery delivery = consumer.nextDelivery();
					String msg = new String(delivery.getBody());
					System.out.println("received message[" + msg + "] from " + RabbitConfig.QUEUENAME + " TIME " +System.currentTimeMillis());
				}
			}
			RabbitUtil.close(conn, channel);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
}
